﻿// project created on 27.07.2007 at 12:47
using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Resources;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;

namespace Gamadus.Engine
{
	public class GamadusEngine : SingletonManager<GamadusEngine>
	{
		public Hashtable GameSettings = new Hashtable();
		public Random random = new Random();
		static string tempDir;
		
		[STAThread]
		public static void Main(string[] args)
        {
			do
			{
				tempDir = Path.GetTempPath()+Path.GetRandomFileName();
			}
			while(Directory.Exists(tempDir));
			tempDir += Path.DirectorySeparatorChar;
			
			AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
			
        	Console.WriteLine("Loading Engine...");
			GamadusEngine.Instance.Go();
        }
		
		private static Assembly OnAssemblyResolve(object sender,ResolveEventArgs e)
		{
			if(!Directory.Exists(tempDir))
				Directory.CreateDirectory(tempDir);
			
			string assemblyFile = e.Name.Substring(0,e.Name.IndexOf(","))+".dll";

        	Console.WriteLine("Unpacking "+assemblyFile+" to "+tempDir+assemblyFile);
        	System.Resources.ResourceManager rm = new System.Resources.ResourceManager("runtime", System.Reflection.Assembly.GetExecutingAssembly());
        	object obj = rm.GetObject(assemblyFile);
        	if(obj == null)
        		throw new FileNotFoundException("Runtime assembly "+assemblyFile+" couldn't be found");
        	BinaryWriter bw;
			byte[] bytes = (byte[])obj;
			bw = new BinaryWriter(new StreamWriter(tempDir+assemblyFile, false).BaseStream);
			bw.Write(bytes);
			bw.Close();
			//load config file if there is one
			obj = rm.GetObject(assemblyFile+".config");
			if(obj != null)
			{
				Console.WriteLine("Unpacking "+assemblyFile+".config to "+tempDir+assemblyFile+".config");
				bytes = (byte[])obj;
				bw = new BinaryWriter(new StreamWriter(tempDir+assemblyFile+".config", false).BaseStream);
				bw.Write(bytes);
				bw.Close();
			}
			return Assembly.LoadFrom(tempDir+assemblyFile);
		}
        
        /// <summary>
        /// Runs the engine by initializing managers for resources and window,
        /// going to the first room and starting the game loop
        /// </summary>
		public void Go()
        {
			try
			{
				
	        	InputMgr.Instance.Init();
	        	ResourceMgr.Instance.Init();
	        	WindowMgr.Instance.Init();
	        	DrawMgr.Instance.Init();
	        	
	        	Console.WriteLine("Managers initialized");
	        	
	        	RoomMgr.Instance.GoToFirst();
				WindowMgr.Instance.Run();
			}
			catch(TypeInitializationException e)
			{
				if(e.InnerException is DllNotFoundException)
				{
					ShowDllError(e.InnerException as DllNotFoundException);
				}
			}
			catch(DllNotFoundException e)
			{
				ShowDllError(e);
			}
        }
        void ShowDllError(DllNotFoundException e)
        {
        	string dll = e.Message.Substring(0,e.Message.IndexOf(".dll"));
			string msg = "Die DLL "+dll+".dll konnte nicht gefunden werden.\nSpiel kann nicht gestartet werden.";
			System.Windows.Forms.MessageBox.Show(msg,"DLL nicht gefunden",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
        }
	}
}